package com.study.samsung.Control;



import com.alibaba.fastjson.JSONObject;
import com.study.samsung.Entity.Classification;
import com.study.samsung.Entity.Commodity;
import com.study.samsung.Entity.Succession;
import com.study.samsung.Entity.User;
import com.study.samsung.Service.GoodsService;
import com.study.samsung.Service.IndexSearchService;
import com.study.samsung.Utils.BaseServlet;
import com.study.samsung.Utils.Pager;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;
import java.util.List;


@WebServlet("/index")
public class IndexServlet extends BaseServlet {
    IndexSearchService iss = new IndexSearchService();
    GoodsService gds = new GoodsService();
    @Override
    public String excute(HttpServletRequest request, HttpServletResponse response) throws Exception {
        request.setCharacterEncoding("utf-8");

        Object user = session.getAttribute("user");
        if (user!=null){
            User nowuser = (User) user;
            Integer uid = nowuser.getUid();
            session.setAttribute("cartcount",gds.cartcount(uid));
        }


            List<Commodity> newgoods = iss.selectnew();
            List<Succession> selectxl = iss.selectxl();

            request.setAttribute("newgoods",newgoods);
            request.setAttribute("selectxl",selectxl);

            return "f:index";


    }

    public String search(HttpServletRequest request, HttpServletResponse response) throws Exception {
        request.setCharacterEncoding("utf-8");
        String searchvalue = request.getParameter("searchvalue");

        Integer pageNo = 1,pageSize = 16;
        Pager pager = iss.search(searchvalue,pageNo,pageSize);

        List<Commodity> newgoods = iss.selectnew();
        List<Succession> selectxl = iss.selectxl();


        request.setAttribute("newgoods",newgoods);
        request.setAttribute("selectxl",selectxl);
        if (pager.getData().size()>0){
            request.setAttribute("pager",pager);
            request.setAttribute("searchvalue",searchvalue);

            return "f:goodsList";
        }else {
            request.setAttribute("searchnull",-1);
            request.setAttribute("searchvalue",searchvalue);

            return "f:goodsList";
        }
    }

    public void fenye(HttpServletRequest request, HttpServletResponse response) throws Exception {
        request.setCharacterEncoding("utf-8");
        PrintWriter out = response.getWriter();
        JSONObject jsonObject = new JSONObject();

        String searchvalue = request.getParameter("searchvalue");
        String pageNo = request.getParameter("pageNo");

        Pager pager = iss.search(searchvalue, Integer.parseInt(pageNo), 16);
        if (pager.getData().size()>0){
            jsonObject.put("pager",pager);
            out.println(jsonObject);
        }else {
            jsonObject.put("searchnull",-1);
            out.println(jsonObject);
        }
    }

    public void getfenye(HttpServletRequest request, HttpServletResponse response) throws Exception {
        request.setCharacterEncoding("utf-8");
        PrintWriter out = response.getWriter();
        String searchvalue = request.getParameter("searchvalue");

        Integer pageNo = 1,pageSize = 16;
        Pager pager = iss.search(searchvalue,pageNo,pageSize);

        if (pager.getData().size()>0){
            out.println(JSONObject.toJSONString(pager));
        }else {
            request.setAttribute("searchnull",-1);
            out.println(-1);
        }
    }

    public void searchtip(HttpServletRequest request, HttpServletResponse response) throws Exception {
        request.setCharacterEncoding("utf-8");
        String searchvalue = request.getParameter("searchvalue");
        System.out.println(searchvalue);
        List<Commodity> commodities = iss.searchTips(searchvalue);
        PrintWriter out = response.getWriter();
        out.println(JSONObject.toJSONString(commodities));
    }

    public String searchli(HttpServletRequest request, HttpServletResponse response) throws Exception {
        request.setCharacterEncoding("utf-8");
        Integer xid = Integer.parseInt(request.getParameter("xid"));
        List<Commodity> commodities = iss.selectxl1(xid);
        return JSONObject.toJSONString(commodities);
    }
}
